<?php

/**
 * @author ntourne
 * @date 23/05/2011
 */

class UserLogin extends User {

		
	function __construct() {
		parent::__construct();
	} 
	
	
	/**
	 * Validate input data for Login
	 * @return true if User's data validates
	 */
	function validateLogin() {
		
		// validate username		
		if (Validation::isEmptyString($this->usernameEmail))
			$this->addMessage(Message::$USERNAME_OR_EMAIL_CANT_EMPTY); // array("type" => MessageType::ERROR, "text" => "User Name or Email cannot be empty");
			
		// validate password
		if (Validation::isEmptyString($this->password))
			$this->addMessage(Message::$PASSWORD_CANT_EMPTY); // array("type" => MessageType::ERROR, "text" => "Password cannot be empty");
		
		return Validation::isEmptyArray($this->messages);
	}
	
	
	/**
	 * Validate input data for Recover password
	 * @return true if User's data validates
	 */
	function validateRecoverPassword() {
		
		// validate email		
		if (Validation::isEmptyString($this->email))
			$this->addMessage(Message::$EMAIL_CANT_EMPTY); // array("type" => MessageType::ERROR, "text" => "Email cannot be empty");
		else if (!Validation::isLessMaxLengthString($this->email, 100))
			$this->addMessage(Message::$EMAIL_LENGTH_LARGER_100_CHARS); // array("type" => MessageType::ERROR, "text" => "Email lenght can not be larger than 100 characters");			
		else if (!Validation::isEmail($this->email))
			$this->addMessage(Message::$EMAIL_NOT_VALID); // array("type" => MessageType::ERROR, "text" => "Email is not valid");
			
		// validate recaptcha code
		if (!Validation::isValidRecaptcha($this->recaptcha_challenge_field, $this->recaptcha_response_field, $this->recaptcha_privatekey))
			$this->addMessage(Message::$CAPTCHA_CODE_INCORRECT); // array("type" => MessageType::ERROR, "text" => "The captcha code wasn't entered correctly");			
		
		return Validation::isEmptyArray($this->messages);
	}
	
	
	/**
	 * Validate input data for Reset password
	 * @return true if User's data validates
	 */
	function validateResetPasswordFromEmail() {
		
		// validate password
		if (Validation::isEmptyString($this->password))
			$this->addMessage(Message::$PASSWORD_CANT_EMPTY); // array("type" => MessageType::ERROR, "text" => "Password cannot be empty");
		else if (!Validation::isLessMaxLengthString($this->password, 50))
			$this->addMessage(Message::$PASSWORD_LENGTH_LARGER_50_CHARS); // array("type" => MessageType::ERROR, "text" => "Password lenght can not be larger than 50 characters");

		// validate repeat password
		if (Validation::isEmptyString($this->repeatpassword))
			$this->addMessage(Message::$REPEAT_NEW_PASSWORD_CANT_EMPTY); // array("type" => MessageType::ERROR, "text" => "Repeat Password cannot be empty");
		else if (!Validation::isLessMaxLengthString($this->repeatpassword, 50))
			$this->addMessage(Message::$REPEAT_NEW_PASSWORD_LENGTH_LARGER_50_CHARS); // array("type" => MessageType::ERROR, "text" => "Repeat Password lenght can not be larger than 50 characters");

		if (!Validation::isEmptyString($this->password) && !Validation::isEmptyString($this->repeatpassword)) {
			if (!Validation::isStringMatch($this->password, $this->repeatpassword))
				$this->addMessage(Message::$BOTH_PASSWORD_DONT_MATCH); // array("type" => MessageType::ERROR, "text" => "Both password don't match");
		}
		
		return Validation::isEmptyArray($this->messages);
	}
	
	
	/**
	 * Validate input data for Reset password
	 * @return true if User's data validates
	 */
	function validateResetPassword() {
		
		// validate password
		if (Validation::isEmptyString($this->password))
			$this->addMessage(Message::$PASSWORD_CANT_EMPTY); // array("type" => MessageType::ERROR, "text" => "Password cannot be empty");
		else if (!Validation::isLessMaxLengthString($this->password, 50))
			$this->addMessage(Message::$PASSWORD_LENGTH_LARGER_50_CHARS); // array("type" => MessageType::ERROR, "text" => "Password lenght can not be larger than 50 characters");

		// validate new password
		if (Validation::isEmptyString($this->newpassword))
			$this->addMessage(Message::$NEW_PASSWORD_CANT_EMPTY); // array("type" => MessageType::ERROR, "text" => "Password cannot be empty");
		else if (!Validation::isLessMaxLengthString($this->newpassword, 50))
			$this->addMessage(Message::$NEW_PASSWORD_LENGTH_LARGER_50_CHARS); // array("type" => MessageType::ERROR, "text" => "Password lenght can not be larger than 50 characters");			
			
		// validate repeat password
		if (Validation::isEmptyString($this->repeatnewpassword))
			$this->addMessage(Message::$REPEAT_NEW_PASSWORD_CANT_EMPTY); // array("type" => MessageType::ERROR, "text" => "Repeat Password cannot be empty");
		else if (!Validation::isLessMaxLengthString($this->repeatnewpassword, 50))
			$this->addMessage(Message::$REPEAT_NEW_PASSWORD_LENGTH_LARGER_50_CHARS); // array("type" => MessageType::ERROR, "text" => "Repeat Password lenght can not be larger than 50 characters");

		if (!Validation::isEmptyString($this->newpassword) && !Validation::isEmptyString($this->repeatnewpassword)) {
			if (!Validation::isStringMatch($this->newpassword, $this->repeatnewpassword))
				$this->addMessage(Message::$BOTH_PASSWORD_DONT_MATCH); // array("type" => MessageType::ERROR, "text" => "Both password don't match");
		}
		
		return Validation::isEmptyArray($this->messages);
	}
	
	
	/**
	 * Validate email of the User
	 * @return true if User's email validates
	 */
	function validateEmail() {
		
		// validate email
		if (Validation::isEmptyString($this->email))
			$this->addMessage(Message::$EMAIL_CANT_EMPTY); // array("type" => MessageType::ERROR, "text" => "Email cannot be empty");
		else if (!Validation::isLessMaxLengthString($this->email, 100))
			$this->addMessage(Message::$EMAIL_LENGTH_LARGER_100_CHARS); // array("type" => MessageType::ERROR, "text" => "Email lenght can not be larger than 100 characters");			
		else if (!Validation::isEmail($this->email))
			$this->addMessage(Message::$EMAIL_NOT_VALID); // array("type" => MessageType::ERROR, "text" => "Email is not valid");
			
		// validate users don't exist with this username and email 
		$users = $this->getUsersByUsernameEmail();
		if (!Validation::isEmptyArray($users))
			$this->addMessage(Message::$USER_ALREADY_EXISTS_CURRENT_EMAIL); // array("type" => MessageType::ERROR, "text" => "A User already exists with the current Email");			
			
		return Validation::isEmptyArray($this->messages);
	}
	
	
	/**
	 * Login user
	 * @return true if User's exists and login is successfull
	 */
	function login() {
		
		// if username + password exists
		if ($this->getUserByUsernamePassword()) {

			// update lastLogin and lastIpAddress
			$this->updateLoginInfo();
			
			$_SESSION['user_logged'] = 1;
			$_SESSION['userid'] = $this->id;
			$_SESSION['username'] = $this->username; 
			
			return true;
			
		} else {
			
			$this->addMessage(Message::$USER_DOESNT_EXIST_USERNAME_PASSWORD); // array("type" => MessageType::ERROR, "text" => "A User doesn't exist for the User Name and Password");
			return false;
		}
		
	}	
	
	
	/**
	 * Logout user
	 */
	function logout() {
		$_SESSION = array();
		session_destroy(); 
	}
	

	/**
	 * Update login info access (lastLogin and lastIpAddress)
	 */
	function updateLoginInfo() {
		$db 	= new ManagerDB();
		$conn 	= $db->getConnection();
		$dbUtil = new DBUtil();
		
		$dbUtil->setSPName("UPD_UserLoginInfo");
		$dbUtil->setParameter($this->id, ParameterDB::INT_TYPE);
		$dbUtil->setParameter($this->lastIpAddress, ParameterDB::VARCHAR_TYPE);
		$dbUtil->setParameter(date('Y-m-d H:i:s'), ParameterDB::DATETIME_TYPE);
		
		$sent = $dbUtil->generateSP();
		$sql = mysql_query($sent, $conn) or die (mysql_error());
		
		$db->close_connection();
		
		return true;
	}

	
	/**
	 * Update password
	 */
	function updatePasswordFromEmail() {
		$db 	= new ManagerDB();
		$conn 	= $db->getConnection();
		$dbUtil = new DBUtil();
		
		$dbUtil->setSPName("UPD_UserPassword");
		$dbUtil->setParameter(Security::prevenirSqlInjection($this->token), ParameterDB::VARCHAR_TYPE);
		$dbUtil->setParameter(md5(Security::prevenirSqlInjection($this->password)), ParameterDB::VARCHAR_TYPE);
		
		$sent = $dbUtil->generateSP();
		$sql = mysql_query($sent, $conn) or die (mysql_error());
		
		$db->close_connection();
		
		return true;
	}
	
	
	/**
	 * Update password
	 */
	function updatePassword() {
		$db 	= new ManagerDB();
		$conn 	= $db->getConnection();
		$dbUtil = new DBUtil();
		
		$dbUtil->setSPName("UPD_UserChangePassword");
		$dbUtil->setParameter($_SESSION['userid'], ParameterDB::VARCHAR_TYPE);
		$dbUtil->setParameter(md5(Security::prevenirSqlInjection($this->password)), ParameterDB::VARCHAR_TYPE);
		$dbUtil->setParameter(md5(Security::prevenirSqlInjection($this->newpassword)), ParameterDB::VARCHAR_TYPE);
		
		$sent = $dbUtil->generateSP();
		$sql = mysql_query($sent, $conn) or die (mysql_error());
		
		$db->close_connection();
		
		return true;
	}
	
	
	
	/**
	 * Update email
	 */
	function updateEmail() {
		$db 	= new ManagerDB();
		$conn 	= $db->getConnection();
		$dbUtil = new DBUtil();
		
		$dbUtil->setSPName("UPD_UserEmail");
		$dbUtil->setParameter($_SESSION['userid'], ParameterDB::VARCHAR_TYPE);
		$dbUtil->setParameter(Security::prevenirSqlInjection($this->email), ParameterDB::VARCHAR_TYPE);
		
		$sent = $dbUtil->generateSP();
		$sql = mysql_query($sent, $conn) or die (mysql_error());
		
		$db->close_connection();
		
		return true;
	}

	
	/**
	 * Return if a User is logged-in in the site 
	 * @return true if the user a logged-in
	 */
	public static function isUserLoggedin() {
		return ( isset($_SESSION['user_logged']) && $_SESSION['user_logged'] == 1 );
	}
	
}
